<?php 
include("../header.inc");
require_once("../DataBase.inc");
require_once("../general_queries.inc");

$center_id = $_GET["center_id"];
$center = DataBase::query("SELECT * FROM interview_center LEFT JOIN province ON province.province_id=interview_center.province_id WHERE interview_center.interview_center_id=".$center_id);
if ($center == false || count($center)==0) die("Invalid center id");
$center = $center[0];

$programs = query_programs()->execute();

function parse_time($str) {
	$i = strpos($str, ":");
	if ($i === false) $i = strpos($str, "h");
	if ($i === false) { $h = $str; $m = "0"; }
	else { $h = substr($str, 0, $i); $m = substr($str, $i+1); }
	if (!ctype_digit($h) || !ctype_digit($m)) return null;
	return array(intval($h),intval($m));
}

if (isset($_POST["create_room"])) {
	$room = trim($_POST["create_room"]);
	if ($room == "") echo "<div style='color:red'>The room cannot be created because you didn't give a name/number</div>";
	else {
		$r = DataBase::select("interview_center_rooms",null,array("interview_center_id"=>$center_id,"room"=>$room));
		if ($r <> false && count($r)>0) echo "<div style='color:red'>The room ".$room." already exists in this interview center</div>";
		else {
			DataBase::insert("interview_center_rooms",array("interview_center_id"=>$center_id,"room"=>$room));
		}
	}
} else if (isset($_POST["new_date"])) {
	$date = $_POST["new_date"];
	$start = $_POST["start_time"];
	$end = $_POST["end_time"];
	$every = $_POST["interview_time"];
	$sel_rooms = array();
	foreach ($_POST as $name=>$value)
		if (substr($name,0,5)=="room_")
			array_push($sel_rooms, substr($name,5));
	$errors = array();
	if ($date=="") array_push($errors, "You didn't specify a date for the new interviews");
	else {
		$today = getdate();
		$today = mktime(0,0,0,$today["mon"],$today["mday"],$today["year"]);
		$date_ts = strtotime($date);
		if ($date_ts < $today) array_push($errors, "The date is in the past!");
	}
	if ($start=="") { $start = null; array_push($errors, "You didn't specify a starting time"); }
	else {
		$start = parse_time($start);
		if ($start == null) array_push($errors, "Invalid start time: format can be like <b>8:30</b> or <b>8</b> (equivalent to 8:00) or <b>8h30</b>");
	}
	if ($end=="") { $end = null; array_push($errors, "You didn't specify an ending time"); }
	else {
		$end = parse_time($end);
		if ($end == null) array_push($errors, "Invalid end time: format can be like <b>8:30</b> or <b>8</b> (equivalent to 8:00) or <b>8h30</b>");
	}
	if ($end <> null && $start <> null && ($end[0] < $start[0] || ($end[0] == $start[0] && $end[1]<=$start[1])))
	{ $end = null; $start = null; array_push($errors, "The ending time is before the starting time !"); }
	if ($every == "") { $every = null; array_push($errors, "You didn't specify an interval time between each interview"); }
	else if (!ctype_digit($every)) { $every = null; array_push($errors, "Invalid interval time: must be a valid number"); }
	else $every = intval($every);
	if (count($sel_rooms)==0) array_push($errors, "You didn't select any room");
	if ($date <> null && $start <> null && $end <> null) {
		$start_time = $start[0].":".$start[1].":00";
		$end_time = $end[0].":".$end[1].":00";
		foreach ($sel_rooms as $room) {
			$res = DataBase::query("SELECT * FROM interview WHERE selection_campaign_id=".$_SESSION["campaign_id"]." AND interview_center_id=".$center_id." AND room='".mysql_escape_string($room)."' AND interview_date='".$date."' AND ((interview_start_time>='".$start_time."' AND interview_start_time<'".$end_time."') OR (interview_end_time>='".$start_time."' AND interview_end_time<'".$end_time."') OR (interview_start_time<'".$start_time."' AND interview_end_time>='".$end_time."'))");
			if ($res<>false && count($res)>0) array_push($errors, "Interviews are already plan during this time for room ".$room);
		}
	}
	$start_minutes = $start[0]*60+$start[1];
	$end_minutes = $end[0]*60+$end[1];
	if ($start_minutes + $every > $end_minutes) array_push($errors, "Invalid times: the starting time + interview time is greater than ending time: no time for an interview!");
	if (count($errors)>0) {
		echo "<div style='color:red'>The new interviews cannot be plan for the following reasons:<ul style='margin:0px'>";
		foreach ($errors as $e) echo "<li>".$e."</li>";
		echo "</ul></div>";
	} else {
		$rows = array();
		for ($time = $start_minutes; $time+$every <= $end_minutes; $time += $every) {
			$start_time_str = floor($time/60).":".($time%60).":00";
			$end_time_str = floor(($time+$every)/60).":".(($time+$every)%60).":00";
			foreach ($sel_rooms as $room) {
				array_push($rows, array(
					"selection_campaign_id"=>$_SESSION["campaign_id"],
					"interview_center_id"=>$center_id, 
					"room"=>$room,
					"interview_date"=>$date,
					"interview_start_time"=>$start_time_str,
					"interview_end_time"=>$end_time_str,
				));
			}			
		}
		DataBase::insert_multiples("interview", $rows);
	}
} else if (isset($_GET["remove_date"])) {
	if (isset($_GET["remove_time"])) {
		$res = DataBase::select("interview", array("interview_id"), array("interview_center_id"=>$center_id,"selection_campaign_id"=>$_SESSION["campaign_id"],"interview_date"=>$_GET["remove_date"],"interview_start_time"=>$_GET["remove_time"]));
		if ($res <> false && count($res)> 0) {
			DataBase::delete("interview", array("interview_center_id"=>$center_id,"selection_campaign_id"=>$_SESSION["campaign_id"],"interview_date"=>$_GET["remove_date"],"interview_start_time"=>$_GET["remove_time"]));
			$where = "";
			foreach ($res as $r) {
				if ($where <> "") $where .= " OR ";
				$where .= "interview_id=".$r["interview_id"];
			}
			DataBase::query("UPDATE applicant_interview SET interview_id=NULL WHERE ".$where);
			DataBase::query("DELETE FROM interviewer WHERE ".$where);
			DataBase::query("DELETE FROM interview_observer WHERE ".$where);
		}
	} else {
		$res = DataBase::select("interview", array("interview_id"), array("interview_center_id"=>$center_id,"selection_campaign_id"=>$_SESSION["campaign_id"],"interview_date"=>$_GET["remove_date"]));
		if ($res <> false && count($res)> 0) {
			DataBase::delete("interview", array("interview_center_id"=>$center_id,"selection_campaign_id"=>$_SESSION["campaign_id"],"interview_date"=>$_GET["remove_date"]));
			$where = "";
			foreach ($res as $r) {
				if ($where <> "") $where .= " OR ";
				$where .= "interview_id=".$r["interview_id"];
			}
			DataBase::query("UPDATE applicant_interview SET interview_id=NULL WHERE ".$where);
			DataBase::query("DELETE FROM interviewer WHERE ".$where);
			DataBase::query("DELETE FROM interview_observer WHERE ".$where);
		}
	}
} else if (isset($_GET["remove_room"])) {
	$res = DataBase::select("interview", array("interview_id"), array("interview_center_id"=>$center_id,"selection_campaign_id"=>$_SESSION["campaign_id"],"room"=>$_GET["remove_room"]));
	DataBase::delete("interview_center_rooms", array("interview_center_id"=>$center_id,"room"=>$_GET["remove_room"]));
	if ($res <> false && count($res)> 0) {
		DataBase::delete("interview", array("interview_center_id"=>$center_id,"selection_campaign_id"=>$_SESSION["campaign_id"],"room"=>$_GET["remove_room"]));
		$where = "";
		foreach ($res as $r) {
			if ($where <> "") $where .= " OR ";
			$where .= "interview_id=".$r["interview_id"];
		}
		DataBase::query("UPDATE applicant_interview SET interview_id=NULL WHERE ".$where);
		DataBase::query("DELETE FROM interviewer WHERE ".$where);
		DataBase::query("DELETE FROM interview_observer WHERE ".$where);
	}
} else if (isset($_GET["assign_from_exam"])) {
	$res = DataBase::select("exam_center",null,array("province_id"=>$center["province_id"],"exam_center_name"=>$center["interview_center_name"]));
	if ($res <> false && count($res)>0) {
		$q = "FROM applicant_exam_room_beta";
		$q .= " LEFT JOIN applicant ON applicant.applicant_id=applicant_exam_room_beta.applicant_id";
		$q .= " LEFT JOIN applicant_interview ON applicant_interview.applicant_id=applicant_exam_room_beta.applicant_id";
		$where = "";
		foreach ($programs as $p) {
			$q .= " LEFT JOIN applicant_eligibility AS eligibility_".$p["program_id"]." ON (eligibility_".$p["program_id"].".applicant_id=applicant.applicant_id AND eligibility_".$p["program_id"].".program_id=".$p["program_id"].")";
			if ($where <> "") $where .= " OR ";
			$where .= "eligibility_".$p["program_id"].".eligible=1";
		}
		$q .= " WHERE applicant_interview.interview_center_id IS NULL AND applicant_exam_room_beta.exam_center_id=".$res[0]["exam_center_id"]." AND applicant.excluded=0 AND applicant.selection_campaign_id=".$_SESSION["campaign_id"]." AND (".$where.")";
		
		$q = "INSERT INTO applicant_interview (applicant_id,interview_center_id) SELECT DISTINCT applicant.applicant_id,".$center_id." ".$q;
		DataBase::query($q);
		echo "<div><i>".DataBase::number_of_affected_rows()." applicant(s) assigned</i></div>";
	}
} else if (isset($_POST["assign_nb"])) {
	$nb = $_POST["assign_nb"];
	$exam_center_id = $_POST["exam_center_id"];
	if ($nb <> "" && $nb > 0 && $exam_center_id <> "") {
		$where = "";
		$q = "FROM exam_center";
		$q .= " LEFT JOIN province ON province.province_id=exam_center.province_id";
		$q .= " LEFT JOIN applicant_exam_room_beta ON applicant_exam_room_beta.exam_center_id=exam_center.exam_center_id";
		$q .= " LEFT JOIN applicant ON applicant.applicant_id=applicant_exam_room_beta.applicant_id";
		foreach ($programs as $p) {
			$q .= " LEFT JOIN applicant_eligibility AS eligibility_".$p["program_id"]." ON (eligibility_".$p["program_id"].".applicant_id=applicant.applicant_id AND eligibility_".$p["program_id"].".program_id=".$p["program_id"].")";
			if ($where <> "") $where .= " OR ";
			$where .= "eligibility_".$p["program_id"].".eligible=1";
		}
		$q .= " LEFT JOIN applicant_interview ON applicant_interview.applicant_id=applicant.applicant_id";
		$q .= " WHERE (".$where.") AND applicant.excluded=0 AND applicant.selection_campaign_id=".$_SESSION["campaign_id"]." AND applicant_interview.interview_center_id IS NULL";
		$q .= " AND exam_center.exam_center_id=".$exam_center_id;
		$q = "INSERT INTO applicant_interview (applicant_id,interview_center_id) SELECT DISTINCT applicant.applicant_id,".$center_id." ".$q." LIMIT ".$nb;
		DataBase::query($q);
		echo "<div><i>".DataBase::number_of_affected_rows()." applicant(s) assigned</i></div>";
	}
} else if (isset($_GET["assign_applicants_to_empty_interviews"])) {
	$interviews_empty = DataBase::query("SELECT interview.interview_id FROM interview LEFT JOIN applicant_interview ON applicant_interview.interview_id=interview.interview_id WHERE interview.interview_center_id=".$center_id." AND interview.selection_campaign_id=".$_SESSION["campaign_id"]." AND applicant_interview.applicant_id IS NULL ORDER BY interview.interview_date,interview.interview_start_time");
	$q = "SELECT applicant.applicant_id";
	foreach ($programs as $p) {
		$q .= ",i".$p["program_id"].".interested AS interested_".$p["program_id"];
		$q .= ",e".$p["program_id"].".eligible AS eligible_".$p["program_id"];
	}
	$q .= " FROM applicant_interview";
	$q .= " LEFT JOIN applicant ON applicant.applicant_id=applicant_interview.applicant_id";
	foreach ($programs as $p) {
		$q .= " LEFT JOIN applicant_interest AS i".$p["program_id"]." ON (i".$p["program_id"].".applicant_id=applicant_interview.applicant_id AND i".$p["program_id"].".program_id=".$p["program_id"].")";
		$q .= " LEFT JOIN applicant_eligibility AS e".$p["program_id"]." ON (e".$p["program_id"].".applicant_id=applicant_interview.applicant_id AND e".$p["program_id"].".program_id=".$p["program_id"].")";
	}
	$q .= " WHERE applicant.excluded=0";
	$q .= " AND applicant.selection_campaign_id=".$_SESSION["campaign_id"];
	$q .= " AND applicant_interview.interview_center_id=".$center_id;
	$q .= " AND applicant_interview.interview_id IS NULL";
	$applicants_not_plan = DataBase::query($q);
	if ($interviews_empty <> false && count($interviews_empty)>0 && $applicants_not_plan <> false) {
		for ($i = 0; $i < count($interviews_empty) && $i < count($applicants_not_plan); $i++) {
			$interview_type = null;
			foreach ($programs as $p) {
				if ($applicants_not_plan[$i]["eligible_".$p["program_id"]] <> 1) continue;
				if ($applicants_not_plan[$i]["interested_".$p["program_id"]] <> 1) continue;
				$interview_type = $p["program_id"];
				break;
			}
			DataBase::update("applicant_interview", array("applicant_id"=>$applicants_not_plan[$i]["applicant_id"]), array("interview_id"=>$interviews_empty[$i]["interview_id"],"interview_type"=>$interview_type));
		}
	}
} else if (isset($_GET["remove_interviewer"])) {
	$username = $_GET["remove_interviewer"];
	$id = $_GET["interview_id"];
	DataBase::update("interview",array("interview_id"=>$id),array("interviewer_username"=>null));
} else if (isset($_GET["remove_observer"])) {
	$username = $_GET["remove_observer"];
	$id = $_GET["interview_id"];
	DataBase::delete("interview_observer",array("interview_id"=>$id,"observer_username"=>$username));
}

?>
<center><span style='font-size:14pt'>Interview center <b><?php echo $center["interview_center_name"]?></b> in <b><?php echo $center["province_name"]?></b> province</span></center>
<br/><br/>
<?php
$rooms = DataBase::select("interview_center_rooms",null,array("interview_center_id"=>$center_id));
if ($rooms == false || count($rooms)==0) echo "<div style='color:red'>No room defined for this interview center</div>";
?>
<div style='border-bottom: 1px solid black; margin-bottom: 20px'>
<form action="?center_id=<?php echo $center_id?>" method='post'>
Create a new room: <input type='text' name='create_room' size=10 maxlength=10/> <button type='submit'>Ok</button>
</form>
</div>
<?php
if ($rooms <> false && count($rooms)>0) {
	
	$nb_interviews = DataBase::query("SELECT COUNT(*) FROM interview WHERE interview_center_id=".$center_id." AND interview.selection_campaign_id=".$_SESSION["campaign_id"]);
	$nb_interviews = $nb_interviews[0][0];
	if ($nb_interviews == 0) echo "<div style='color:red'>No interview plan</div>";
	
	echo "<div style='border-bottom: 1px solid black; margin-bottom: 20px'>";
	echo "<form action='?center_id=".$center_id."' method='post'>";
	echo "Plan interviews:";
	echo "<br/> &nbsp; - ";
	echo " on date <input type='text' size=10 readonly='readonly' name='new_date' onclick=\"displayDatePicker('new_date')\"/>";
	echo " from <input type='text' name='start_time' size=5 maxlength=5/>";
	echo " to <input type='text' name='end_time' size=5 maxlength=5/>";
	echo " every <input type='text' name='interview_time' size=3 maxlength=3/>minutes";
	echo "<br/> &nbsp; - ";
	echo " for all selected rooms:";
	foreach ($rooms as $room)
		echo " <input type='checkbox' name=\"room_".$room["room"]."\" checked='checked'/> ".$room["room"];
	echo "<br/> <button type='submit'>Ok</button>";
	echo "</form>";
	echo "</div>";
	
	$nb_assigned = DataBase::query("SELECT COUNT(*) FROM applicant_interview LEFT JOIN applicant ON applicant.applicant_id=applicant_interview.applicant_id WHERE applicant.excluded=0 AND applicant.selection_campaign_id=".$_SESSION["campaign_id"]." AND applicant_interview.interview_center_id=".$center_id);
	$nb_assigned = $nb_assigned[0][0];
	echo $nb_assigned." applicant(s) assigned to this interview center<br/>";
	$res = DataBase::select("exam_center",null,array("province_id"=>$center["province_id"],"exam_center_name"=>$center["interview_center_name"]));
	if ($res <> false && count($res)>0) {
		echo "<ul style='margin:0px'>";
		$where = "";
		$q = "FROM exam_center";
		$q .= " LEFT JOIN province ON province.province_id=exam_center.province_id";
		$q .= " LEFT JOIN applicant_exam_room_beta ON applicant_exam_room_beta.exam_center_id=exam_center.exam_center_id";
		$q .= " LEFT JOIN applicant ON applicant.applicant_id=applicant_exam_room_beta.applicant_id";
		foreach ($programs as $p) {
			$q .= " LEFT JOIN applicant_eligibility AS eligibility_".$p["program_id"]." ON (eligibility_".$p["program_id"].".applicant_id=applicant.applicant_id AND eligibility_".$p["program_id"].".program_id=".$p["program_id"].")";
			if ($where <> "") $where .= " OR ";
			$where .= "eligibility_".$p["program_id"].".eligible=1";
		}
		$q .= " LEFT JOIN applicant_interview ON applicant_interview.applicant_id=applicant.applicant_id";
		$q .= " WHERE (".$where.") AND applicant.excluded=0 AND applicant.selection_campaign_id=".$_SESSION["campaign_id"]." AND applicant_interview.interview_center_id IS NULL";
		$q = "SELECT exam_center.exam_center_id,exam_center.exam_center_name,province.province_name,COUNT(DISTINCT applicant.applicant_id) ".$q." GROUP BY exam_center.exam_center_id";
		$res2 = DataBase::query($q);
		if ($res2 == false) $res2 = array();
		$exam_centers = array();
		foreach ($res2 as $r) {
			if ($r[3]==0) continue; // no applicant
			array_push($exam_centers, $r);
		}
		if (count($exam_centers)==0) {
			echo "<li>No more applicant can be assigned</li>";			
		} else {
			$q = "SELECT COUNT(DISTINCT applicant.applicant_id) FROM applicant_exam_room_beta";
			$q .= " LEFT JOIN applicant ON applicant.applicant_id=applicant_exam_room_beta.applicant_id";
			$q .= " LEFT JOIN applicant_interview ON applicant_interview.applicant_id=applicant_exam_room_beta.applicant_id";
			$where = "";
			foreach ($programs as $p) {
				$q .= " LEFT JOIN applicant_eligibility AS eligibility_".$p["program_id"]." ON (eligibility_".$p["program_id"].".applicant_id=applicant.applicant_id AND eligibility_".$p["program_id"].".program_id=".$p["program_id"].")";
				if ($where <> "") $where .= " OR ";
				$where .= "eligibility_".$p["program_id"].".eligible=1";
			}
			$q .= " WHERE applicant_interview.interview_center_id IS NULL AND applicant_exam_room_beta.exam_center_id=".$res[0]["exam_center_id"]." AND applicant.excluded=0 AND applicant.selection_campaign_id=".$_SESSION["campaign_id"]." AND (".$where.")";
			$nb_from_exam_center = DataBase::query($q);
			$nb_from_exam_center = $nb_from_exam_center[0][0];
			echo "<li>This interview center was also an exam center: ";
			if ($nb_from_exam_center == 0) echo "but no applicant can be assigned from this exam center because there was no exam for this selection campaign, or applicants from this center are not eligible for any program, or all are already assigned to an interview center";
			else echo "<a href='?center_id=".$center_id."&assign_from_exam=yes'>assign the ".$nb_from_exam_center." applicant(s) from this exam center who are eligible for at least one program and are still not assigned to an interview center</a>";
			echo "</li>";
			echo "<li>";
			echo "<form action='?center_id=".$center_id."' method='post'>";
			echo "Assign <input type='text' name='assign_nb' value='0' size=4 maxlength=4 style='text-align:right'/> applicant(s) from exam center ";
			echo "<select name='exam_center_id'>";
			echo "<option value='' selected='selected'> -- Please select an exam center -- </option>";
			foreach ($exam_centers as $r)
				echo "<option value='".$r[0]."'>".$r[2].": ".$r[1]." (".$r[3]." applicants can be assigned)</option>";
			echo "</select>";
			echo " <button type='submit'>Ok</button>";
			echo "<br/><i>Note: exam centers shown are only centers where there are eligible applicants who are not yet assigned to an interview center</i>";
			echo "</form>";
			echo "</li>";
			echo "<li>";
			echo "Assign applicants manually: see the <a href='' onclick='view_applicants_to_assign();return false'>list of eligible applicants not yet assigned to an interview center</a>";
			echo "</li>";
		}
		echo "</ul>";
	}

	$nb_plan = DataBase::query("SELECT COUNT(*) FROM applicant_interview LEFT JOIN applicant ON applicant.applicant_id=applicant_interview.applicant_id WHERE applicant.excluded=0 AND applicant.selection_campaign_id=".$_SESSION["campaign_id"]." AND applicant_interview.interview_center_id=".$center_id." AND applicant_interview.interview_id IS NOT NULL");
	$nb_plan = $nb_plan[0][0];
	$nb_not_plan = $nb_assigned-$nb_plan;
	if ($nb_not_plan == 0)
		echo "<span style='color:green'>All applicants assigned to this interview center have an interview plan.</span>";
	else
		echo "<span style='color:red'>".$nb_not_plan." applicant(s) assigned to this interview center do not have an interview plan.</span>";
	echo "<br/>";
	
	$nb_interviews_empty = DataBase::query("SELECT COUNT(*) FROM interview LEFT JOIN applicant_interview ON applicant_interview.interview_id=interview.interview_id WHERE interview.interview_center_id=".$center_id." AND interview.selection_campaign_id=".$_SESSION["campaign_id"]." AND applicant_interview.applicant_id IS NULL");
	$nb_interviews_empty = $nb_interviews_empty[0][0];
	if ($nb_interviews_empty == 0)
		echo "All interviews plan have an applicant assigned<br/>";
	else {
		echo $nb_interviews_empty." interviews are plan but have no applicant assigned<br/>";
		if ($nb_not_plan > 0) {
			$nb_possible = $nb_not_plan > $nb_interviews_empty ? $nb_interviews_empty : $nb_not_plan;
			echo "<ul style='margin:0px'><li>";
			echo "<a href='?center_id=".$center_id."&assign_applicants_to_empty_interviews'>Assign ".$nb_possible." applicants to an interview</a>";
			echo "</li></ul>";
		}
	}
	
	echo "<br/>";
	
	$q = "";
	foreach ($rooms as $room) {
		if ($q<>"") $q.=" OR ";
		$q .= "interview.room='".mysql_escape_string($room["room"])."'";
	}
	$q = "SELECT interview.*,applicant.*,staff.first_name,staff.last_name,applicant_interview.interview_type FROM interview LEFT JOIN applicant_interview ON applicant_interview.interview_id=interview.interview_id LEFT JOIN applicant ON applicant.applicant_id=applicant_interview.applicant_id LEFT JOIN staff ON staff.username=interview.interviewer_username WHERE interview.interview_center_id=".$center_id." AND interview.selection_campaign_id=".$_SESSION["campaign_id"]." AND (".$q.")";
	$q .= " ORDER BY interview.interview_date,interview.interview_start_time";
	$res = DataBase::query($q);
	if ($res === false) $res = array();
	$interviews = array();
	foreach ($res as $r) {
		$date = $r["interview_date"];
		$time = $r["interview_start_time"];
		if (!isset($interviews[$date])) $interviews[$date] = array();
		if (!isset($interviews[$date][$time])) $interviews[$date][$time] = array();
		$interviews[$date][$time][$r["room"]] = $r;
	}
	
	echo "<table cellspacing=0 cellpadding=2 border=1 style='border-collapse:collapse;empty-cells:show'>";
	foreach ($interviews as $date=>$list_by_date) {
		echo "<tr><td colspan=".(1+count($rooms)*4)." align='center' style='background-color:#A0C0E0'>";
		echo "<b>Date:</b> ";
		echo "<a href='' onclick=\"if (!confirm('Are you sure you want to remove all interviews plan for this date for all the rooms ?')) return false; location.href = '?center_id=".$center_id."&remove_date=".urlencode($date)."'; return false\"><img src='".$_SESSION["root_url"]."/images/trash.gif' border=0 hspace=1 vspace=0 style='vertical-align:bottom'/></a>";
		echo $date;
		echo "</td></tr>";
		echo "<tr>";
		echo "<th rowspan=2 style='background-color:#C0C0FF' valign='bottom'>Time</th>";
		foreach ($rooms as $room) {
			echo "<th colspan=4 style='background-color:#C0C0FF'>";
			echo "<a href='' onclick=\"if (!confirm('Are you sure you want to remove all interviews plan for this room ?')) return false; location.href = '?center_id=".$center_id."&remove_room=".urlencode($room["room"])."'; return false\"><img src='".$_SESSION["root_url"]."/images/trash.gif' border=0 hspace=1 vspace=0 style='vertical-align:bottom'/></a>";
			echo "Room ".$room["room"];
			echo "</th>";
		}
		echo "</tr>";
		echo "<tr>";
		foreach ($rooms as $room) {
			echo "<th style='background-color:#C0C0FF'>Applicant</th>";
			echo "<th style='background-color:#C0C0FF'>Type</th>";
			echo "<th style='background-color:#C0C0FF'>Interviewers<a href='assign_staff.php?center_id=".$center_id."&role=interviewer&date=".urlencode($date)."&room=".urlencode($room["room"])."'><img src='".$_SESSION["root_url"]."/images/small_add.gif' hspace=0 vspace=0 style='vertical-align:middle' border=0/></a></th>";
			echo "<th style='background-color:#C0C0FF'>Observers<a href='assign_staff.php?center_id=".$center_id."&role=observer&date=".urlencode($date)."&room=".urlencode($room["room"])."'><img src='".$_SESSION["root_url"]."/images/small_add.gif' hspace=0 vspace=0 style='vertical-align:middle' border=0/></a></th>";
		}
		echo "</tr>";
		echo "<tr>";
		$i_time = 0;
		foreach ($list_by_date as $time=>$rooms_by_time) {
			if ($i_time > 0) echo "<tr>";
			echo "<td>";
			echo "<a href='' onclick=\"if (!confirm('Are you sure you want to remove all interviews plan for this date and at this time for all the rooms ?')) return false; location.href = '?center_id=".$center_id."&remove_date=".urlencode($date)."&remove_time=".urlencode($time)."'; return false\"><img src='".$_SESSION["root_url"]."/images/trash.gif' border=0 hspace=1 vspace=0 style='vertical-align:bottom'/></a>";
			echo $time;
			echo "</td>";
			foreach ($rooms as $room) {
				if (!isset($rooms_by_time[$room["room"]])) {
					echo "<td colspan=3 style='background-color:#C0C0C0'></td>";
				} else {
					$r = $rooms_by_time[$room["room"]];
					if ($r["applicant_firstname"]<>null) {
						echo "<td>";
						echo $r["applicant_selection_id"].": ";
						echo $r["applicant_firstname"]." ".$r["applicant_lastname"];
						echo "</td>";
						echo "<td>";
						if ($r["interview_type"]<>null) foreach ($programs as $p)  if ($p["program_id"]==$r["interview_type"]) { echo $p["program_name"]; break; }
						echo "</td>";
					} else
						echo "<td></td><td></td>";
					echo "<td>";
					if ($r["interviewer_username"] == null) {
                                            echo "<span style='color:red'>Nobody</span>";
                                            echo "<a href='assign_staff.php?center_id=".$center_id."&role=interviewer&interview_id=".$r["interview_id"]."'><img src='".$_SESSION["root_url"]."/images/small_add.gif' hspace=0 vspace=0 style='vertical-align:middle' border=0/></a>";
                                        } else {
                                            echo $r["first_name"]." ".$r["last_name"];
                                            echo "<a href='?center_id=".$center_id."&remove_interviewer=".urlencode($r["interviewer_username"])."&interview_id=".$r["interview_id"]."'><img src='".$_SESSION["root_url"]."/images/small_remove.gif' hspace=1 vspace=0 style='vertical-align:middle' border=0/></a>";
                                        }
					echo "</td>";
					echo "<td>";
					$observers = DataBase::query("SELECT * FROM interview_observer LEFT JOIN staff ON interview_observer.observer_username=staff.username WHERE interview_id=".$r["interview_id"]);
					if ($observers == false || count($observers)==0)
						echo "Nobody";
					else foreach ($observers as $int) {
						echo $int["first_name"]." ".$int["last_name"];
						echo "<a href='?center_id=".$center_id."&remove_observer=".urlencode($int["observer_username"])."&interview_id=".$r["interview_id"]."'><img src='".$_SESSION["root_url"]."/images/small_remove.gif' hspace=1 vspace=0 style='vertical-align:middle' border=0/></a>";
						echo "<br/>";
					}
					echo "<a href='assign_staff.php?center_id=".$center_id."&role=observer&interview_id=".$r["interview_id"]."'><img src='".$_SESSION["root_url"]."/images/small_add.gif' hspace=0 vspace=0 style='vertical-align:middle' border=0/></a>";
					echo "</td>";
				}
			}
			if ($i_time > 0) echo "</tr>";
			$i_time++;
		}
		echo "</tr>";
	}
	echo "</table>";
} 
?>
<br/>
<a href='centers.php'>Go back to the list of interview centers</a>
<?php include $_SESSION["root_path"]."/applicants/view_script.inc"?>
<script type='text/javascript'>
function view_applicants_to_assign() {
	view_applicants({
		"applicant.applicant_selection_id":false,
		"applicant.applicant_firstname":false,
		"applicant.applicant_lastname":false,
		"applicant.applicant_khmer_name":false,
		"applicant.sex":false,
		"applicant.applicant_origin_province_id#province_name":false,
		"applicant.applicant_parents_province_id#province_name":false,
		"applicant.high_school_id#high_school_name":false,
		"applicant.ngo_id#ngo_name":false,
		"applicant_exam_room_beta.exam_center_id#province_id#province_name":false,
		"applicant_exam_room_beta.exam_center_id#exam_center_name":false,
		"applicant_interview.interview_center_id#province_id#province_name":false,
		"applicant_interview.interview_center_id#interview_center_name":true
	},{
		<?php
			foreach ($programs as $p)
				echo "\"applicant_eligibility.eligible/program_id/".$p["program_id"]."\":\"='1'\",";  
		?>
		"applicant_interview.interview_center_id#interview_center_name":" IS NULL"
	},{
	<?php
	for ($i = 1; $i < count($programs); $i++)
		 echo "\"applicant_eligibility.eligible/program_id/".$programs[$i]["program_id"]."\":\"applicant_eligibility.eligible/program_id/".$programs[$i-1]["program_id"]."\",";
	?>
	});
}
</script>
<?php include("../footer.inc")?>